package com.rykj.qxj.rest;

import com.github.pagehelper.PageInfo;
import com.rykj.qxj.annotation.EnableSystemLog;
import com.rykj.qxj.server.model.entity.DataMart;
import com.rykj.qxj.server.services.IDataMartService;
import com.rykj.qxj.util.RegexUtils;
import com.rykj.qxj.util.Result;
import com.techindata.cacheredis.CacheZSet;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据集市
 */
@Log4j
@RestController
@RequestMapping("/v1/dataMart")
@Api(tags="数据集市管理")
public class DataMartController {

    @Autowired
    private IDataMartService dataMartService;
    //创建
    @PostMapping("/saveDataMart")
    @ApiOperation(value="添加目录分类",notes ="添加目录分")
    @EnableSystemLog(value="添加目录分类",fullPath ="/v1/dataMart/saveDataMart")
    public Result saveDataMart(@RequestBody DataMart dataMart){
        try{
            dataMartService.saveDataMart(dataMart);
            return new Result(true,"添加成功");
        }catch (Exception e){
            log.error("添加失败", e);
            return new Result(false, "添加失败");
        }
    }

    @PostMapping("/updateDataMart")
    @ApiOperation(value="修改集市",notes ="修改集市")
    @EnableSystemLog(value="修改集市",fullPath ="/v1/dataMart/updateDataMart")
    public Result updateDataMart(@RequestBody DataMart dataMart){
        try{
            dataMartService.updateDataMart(dataMart);
            return new Result(true,"修改成功");
        }catch (Exception e){
            log.error("修改失败", e);
            return new Result(false, "修改失败");
        }
    }

    //删除
    @GetMapping("/deleteDataMart")
    @ApiOperation(value="删除集市",notes ="删除集市")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dataMartId", value = "集市id主键",required = true,paramType = "query")
    })
    @EnableSystemLog(value="删除集市",fullPath ="/v1/dataMart/deleteDataMart")
    public Result deleteDataMart(@RequestParam(value = "dataMartId") Integer dataMartId){
        try{
            dataMartService.deleteDataMart(dataMartId);
            return new Result(true,"删除成功");
        }catch (Exception e){
            log.error("删除失败", e);
            return new Result(false, "删除失败");
        }
    }

    @GetMapping("/getDataMartListTree")
    @ApiOperation(value="获取分类目录树",notes ="获取分类目录树")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classLib", value = "类库",required=true,paramType = "query"),
            @ApiImplicitParam(name = "martName", value = "目录名称",paramType = "query")
    })
    public Result getDataMartListTree(@RequestParam(value = "classLib") String classLib,
                                      @RequestParam(value = "martName",required=false) String martName){
        try{
            int parentId =0;
            List<Map<String, Object>> dataMarts=  dataMartService.getDataMartListTree(parentId,classLib,martName);
            return new Result(dataMarts);
        }catch (Exception e){
            log.error("获取集市目录失败", e);
            return new Result(false, "获取集市目录失败");
        }
    }

    @GetMapping("/getDataMartList")
    @ApiOperation(value="分页获取数据目录",notes ="分页获取数据目录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "显示行数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "classLib", value = "类库",paramType = "query"),
            @ApiImplicitParam(name = "martName", value = "目录名称",paramType = "query")
    })
    public Result getDataMartList(@RequestParam(value = "pageNum") Integer pageNum,
                                       @RequestParam(value = "pageSize") Integer pageSize,
                                       @RequestParam(value = "classLib",required=false) String classLib,
                                       @RequestParam(value = "martName",required=false) String martName){
        try{
            PageInfo pager =  dataMartService.getDataMartList(pageNum,pageSize,classLib,martName);
            return new Result(pager);
        }catch (Exception e){
            log.error("分页获取数据目录失败", e);
            return new Result(false, "分页获取数据目录失败");
        }
    }

    @GetMapping("/getCountApiTotal")
    @ApiOperation(value="统计接口总数",notes ="统计接口总数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classLib", value = "类库:主题类:subject;业务类:business",paramType = "query"),
            @ApiImplicitParam(name = "catalogId", value = "分类目录id",paramType = "query")
    })
    public Result getCountApiTotal(@RequestParam(value = "classLib",required = false) String classLib,
                                   @RequestParam(value = "catalogId",required = false) Integer catalogId){
        try{
            Integer apiTotal  = dataMartService.getCountApiTotal(classLib,catalogId);
            return new Result(apiTotal);
        }catch (Exception e){
            log.error("统计接口总数失败", e);
            return new Result(false, "统计接口总数失败");
        }
    }

    @GetMapping("/getThisMonthApiTotal")
    @ApiOperation(value="统计本月接口总数",notes ="统计本月接口总数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classLib", value = "类库:主题类:subject;业务类:business",paramType = "query"),
            @ApiImplicitParam(name = "catalogId", value = "分类目录id",paramType = "query")
    })
    public Result getThisMonthApiTotal(@RequestParam(value = "classLib",required = false) String classLib,
                                     @RequestParam(value = "catalogId",required = false) Integer catalogId){
        try{
            Integer apiTotal  = dataMartService.getThisMonthApiTotal(classLib,catalogId);
            return new Result(apiTotal);
        }catch (Exception e){
            log.error("统计本月接口总数失败", e);
            return new Result(false, "统计本月接口总数失败");
        }
    }

    @GetMapping("/getDataMartCount")
    @ApiOperation(value="按月统计12个月内访问总数",notes ="按月统计12个月内访问总数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classLib", value = "类库:主题类:subject;业务类:business",paramType = "query"),
            @ApiImplicitParam(name = "catalogId", value = "分类目录id",paramType = "query"),
            @ApiImplicitParam(name = "applyId", value = "应用id",paramType = "query"),
            @ApiImplicitParam(name = "apiId", value = "接口id",paramType = "query")
    })
    public Result getDataMartCount(@RequestParam(value = "classLib",required = false) String classLib,
                                   @RequestParam(value = "catalogId",required = false) Integer catalogId,
                                   @RequestParam(value = "applyId",required = false) Integer applyId,
                                   @RequestParam(value = "apiId",required = false) Integer apiId){
        try{
            Map<String,Object> paraMap = new HashMap<>();
            paraMap.put("countType",classLib);
            paraMap.put("catalogId",catalogId);
            paraMap.put("applyId",applyId);
            paraMap.put("apiId",apiId);
            List<Map<String,Object>> retMap = dataMartService.getDataMartCount(paraMap);
            return new Result(retMap);
        }catch (Exception e){
            log.error("按月统计12个月内访问总数失败", e);
            return new Result(false, "按月统计12个月内访问总数失败");
        }
    }

    @GetMapping("/getCatalogCountTopTen")
    @ApiOperation(value="分类目录下的接口访问量前10统计",notes ="分类目录下的接口访问量前10统计")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classLib", value = "类库:主题类:subject;业务类:business",paramType = "query"),
            @ApiImplicitParam(name = "catalogId", value = "分类目录id",paramType = "query")
    })
    public Result getCatalogCountTopTen(@RequestParam(value = "classLib",required = false) String classLib,
                                   @RequestParam(value = "catalogId",required = false) Integer catalogId){
        try{
            Map<String,Object> paraMap = new HashMap<>();
            paraMap.put("classLib",classLib);
            paraMap.put("catalogId",catalogId);
            List<Map<String,Object>> retMap = dataMartService.getCatalogCountTopTen(paraMap);
            return new Result(retMap);
        }catch (Exception e){
            log.error("分类目录下的接口访问量前10统计失败", e);
            return new Result(false, "分类目录下的接口访问量前10统计失败");
        }
    }


}
